package com.tarena.lbs.coupon.service.impl;

import com.alibaba.nacos.api.model.v2.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.repository.CouponCodeRepository;
import com.tarena.lbs.coupon.dao.repository.CouponRepository;
import com.tarena.lbs.coupon.dao.repository.StockRepository;
import com.tarena.lbs.coupon.dao.repository.UserCouponsRepository;
import com.tarena.lbs.coupon.pojo.param.CouponParam;
import com.tarena.lbs.coupon.pojo.query.UserCouponCodeQuery;
import com.tarena.lbs.coupon.pojo.query.UserCouponQuery;
import com.tarena.lbs.coupon.pojo.vo.CouponVO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import com.tarena.lbs.coupon.service.CouponService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Transactional
@Slf4j
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponRepository couponRepository;
    @Autowired
    private StockRepository stockRepository;
    @Autowired
    private UserCouponsRepository userCouponsRepository;
    @Autowired
    private CouponCodeRepository couponCodeRepository;
    @Override
    public CouponVO detail(Integer id) {
        log.debug("查询优惠券详情,入参:{}",id);
        CouponVO detail = couponRepository.detail(id);
        log.debug("查询优惠券详情成功,入参:{},出参:{}",id,detail);
        return detail;
    }

    @Override
    public PageResult<CouponVO> pageList() {
        log.debug("查询优惠券列表");
        PageResult<CouponVO> page = couponRepository.pageList();
        log.debug("查询优惠券列表成功,出参:{}",page);
        return page;
    }

    @Override
    public void addCoupon(CouponParam couponParam) {
        log.debug("新增优惠券,入参:{}",couponParam);
        CouponVO couponVO = new CouponVO();
        BeanUtils.copyProperties(couponParam, couponVO);
        couponVO.setStatus(1);
        couponVO.setEnableStatus(1);
        couponVO.setCreateAt(new java.util.Date(System.currentTimeMillis()));
        couponVO.setUpdateAt(new java.util.Date(System.currentTimeMillis()));
        couponVO.setUsageNum(0);
        couponRepository.addCoupon(couponParam);
        stockRepository.addStock(couponParam.getId(), couponParam.getMaxUsageLimit());
        couponCodeRepository.addStock(couponParam.getId(), couponParam.getMaxUsageLimit());
        couponCodeRepository.updateStock(couponParam.getId(), couponParam.getMaxUsageLimit());
        couponRepository.addStock(couponParam.getId(), couponParam.getMaxUsageLimit());
        log.debug("新增优惠券成功");
    }

    @Override
    public PageResult<UserCouponsVO> receiveList(UserCouponQuery couponQuery) {
        log.debug("查询用户优惠券列表,入参:{}",couponQuery);
        PageResult<UserCouponsVO> page = couponRepository.receiveList(couponQuery);
        page.setPageNo(couponQuery.getPageNo());
        page.setPageSize(couponQuery.getPageSize());
        page.setTotal(couponRepository.pageList().getTotal());
        page.setObjects(page.getObjects());
        log.debug("查询用户优惠券列表成功,出参:{}",page);
        return page;
    }

    @Override
    public UserCouponsVO receiveDetail(UserCouponCodeQuery query) {
        log.debug("查询用户优惠券详情,入参:{}",query);
        UserCouponsVO detail = couponRepository.receiveDetail(query);
        log.debug("查询用户优惠券详情成功,出参:{}",detail);
        return detail;
    }

    @Override
    public void receiveCoupon(UserCouponsParam param) {
        log.debug("领取优惠券,入参:{}",param);
        couponCodeRepository.addStock(param.getCouponId(), param.getMaxUsageLimit());
        couponCodeRepository.updateStock(param.getCouponId(), param.getMaxUsageLimit());
        couponRepository.receiveCoupon(param);
        couponRepository.addStock(param.getCouponId(), param.getMaxUsageLimit());
        log.debug("领取优惠券成功");
    }

    @Override
    public void addStock(Integer couponId, Object maxUsageLimit) {
        log.debug("新增库存,入参:{}",couponId);
        couponCodeRepository.addStock(couponId, (Integer)maxUsageLimit);
        couponCodeRepository.updateStock(couponId, (Integer)maxUsageLimit);
        stockRepository.addStock(couponId, (Integer)maxUsageLimit);
        log.debug("新增库存成功");
    }


}
